/**
 * Copyright 2011 Sibaozuan(superskysbz@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package summary.android.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
 *  创建一个以默认模式即Standard模式启动的Activity
 * @author Sibaozuan(superskysbz@gmail.com)
 *
 */
public class StandardActivity extends Activity {
	private Button btnStandard, btnRoot;
	private TextView textView;
	private Intent receivedIntent;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		textView = new TextView(this);
		textView.setTextColor(Color.BLACK);
		textView.setMovementMethod(ScrollingMovementMethod.getInstance());
		textView.setText(Constant.STANDARD_FLAG+ "onCreate()\n");
		textView.append(this + "\n");
		
		receivedIntent = getIntent();
		String str = receivedIntent.getStringExtra(Constant.PROMPT);
		if (str != null) {
			textView.append(str);
		}

		btnStandard = new Button(this);
		btnRoot = new Button(this);

		btnRoot.setText("go to RootActivity");
		btnStandard.setText("go to StandardActivity");

		btnStandard.setOnClickListener(new MyOnClickListener());
		btnRoot.setOnClickListener(new MyOnClickListener());

		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);
		layout.setBackgroundColor(Color.WHITE);
		layout.addView(btnStandard);
		layout.addView(btnRoot);
		layout.addView(textView);
		setContentView(layout);
	}

	@Override
	protected void onStart() {
		super.onStart();
		addFlag("onStart()\n");
	}

	@Override
	protected void onResume() {
		super.onResume();
		addFlag("onResume()\n");
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		addFlag("onRestart()\n");
	}

	private void addFlag(String methodName) {
		textView.append(Constant.STANDARD_FLAG + methodName);
		Intent it = getIntent();
		if (it != receivedIntent) {
			String str = it.getStringExtra(Constant.PROMPT);
			if (str != null) {
				textView.append(str);
			}
		}
	}

	/**
	 *
	 * @author Sibaozuan (sibaozuan-hlep@163.com)
	 */
	class MyOnClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			if (v == btnRoot) {
				Intent standardIntent = new Intent();
				standardIntent.setClass(StandardActivity.this, RootActivity.class);
				standardIntent.putExtra(Constant.PROMPT, textView.getText().toString());
				startActivity(standardIntent);
			} else if (v == btnStandard) {
				Intent rootIntent = new Intent();
				rootIntent.setClass(StandardActivity.this, StandardActivity.class);
				rootIntent.putExtra(Constant.PROMPT, textView.getText().toString());
				startActivity(rootIntent);
			}
		}
	}
}